﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPG.SharePoint.Workflow.Controls.Design;

namespace TPG.SharePoint.Workflow.Controls
{
    [ToolboxData("<{0}:InputForm runat=\"server\"></{0}:InputForm>"),
    Designer(typeof(InputFormDesigner)),
    ControlBuilder(typeof(InputFormBuilder)),
    ParseChildren(typeof(FormSection)),
    PersistChildren(true)]
    public class InputForm
        : WebControl
    {
        [DefaultValue(-1),
        Category("Appearance"),
        Description("The spacing between table cells.")]
        public int CellSpacing
        {
            get
            {
                if (ControlStyleCreated == false)
                {
                    return -1;
                }
                return ((TableStyle)ControlStyle).CellSpacing;
            }
            set
            {
                ((TableStyle)ControlStyle).CellSpacing = value;
            }
        }

        [DefaultValue(-1),
        Category("Appearance"),
        Description("The padding inside each table cell.")]
        public int CellPadding
        {
            get
            {
                if (ControlStyleCreated == false)
                {
                    return -1;
                }
                return ((TableStyle)ControlStyle).CellPadding;
            }
            set
            {
                ((TableStyle)ControlStyle).CellPadding = value;
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Table; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
        }

        protected override ControlCollection CreateControlCollection()
        {
            return new InputFormControlCollection(this);
        }

        protected override Style CreateControlStyle()
        {
            TableStyle tableStyle = new TableStyle(ViewState);
            tableStyle.CellPadding = 0;
            tableStyle.CellSpacing = 0;
            tableStyle.Width = Unit.Percentage(100);
            tableStyle.CssClass = "ms-propertysheet";
            return tableStyle;
        }

        protected override void AddParsedSubObject(object obj)
        {
            FormSection section = obj as FormSection;
            if (section == null)
            {
                throw new ArgumentException();
            }
            base.AddParsedSubObject(section);
        }

        class InputFormControlCollection
            : ControlCollection
        {
            internal InputFormControlCollection(InputForm owner)
                : base(owner)
            {
            }

            public override void Add(Control child)
            {
                FormSection section = child as FormSection;
                if (section == null)
                {
                    throw new ArgumentException();
                }
                base.Add(child);
            }

            public override void AddAt(int index, Control child)
            {
                FormSection section = child as FormSection;
                if (section == null)
                {
                    throw new ArgumentException();
                }
                base.AddAt(index, child);
            }
        }
    }
}
